<?php
/**
 * 记录所有的库存的表：存储库存的基本信息
 * String stock_id：库存ID
 * String title：产品名
 * String en_title：英文标题
 * String sku：唯一sku
 * String price：价格
 * String is_forlong：是否永不下架
 * String add_time：最早添加时间
 * String update_time：最新更新时间
 */
class App_Model_Stock extends App_Model_Abstract
{
	public function init()
	{
		parent::init();
		$this->table = App_Db_Table::factory('Stock');
	}
	
	public function add($row)
	{
		return $this->table->insert($row);
	}
	
	public function update($row, $id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('stock_id = ?', $id);
		
		return $this->table->update($row, $where);
	}
	
	public function updateBysku($row, $sku)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('sku = ?', $sku);
	
		return $this->table->update($row, $where);
	}
	
	public function delete($id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('stock_id = ?', $id);
		
		return $this->table->delete($where);
	}
	
	/**获取当前库存，永不下架为是的，永不下架为否的且理论库存大于0的*/
	public function currentstock()
	{
		$sql1 = 'select s.sku, s.title, s.en_title, s.is_forlong, v.value, v.size from stock_value v inner join stock s on s.sku=v.sku where s.is_forlong = 1 order by v.sku';
		$res1 = $this->table->getAdapter()->fetchAll($sql1);
		$sql2 = 'select s.sku, s.title, s.en_title, s.is_forlong, v.value, v.size from stock_value v inner join stock s on s.sku=v.sku where s.is_forlong = 0 and v.value > 0 order by v.sku';
		$res2 = $this->table->getAdapter()->fetchAll($sql2);
		return array_merge($res1,$res2);
	}
	
	public function existSku($sku)
	{
		return $this->table->fetchRow(array('sku = ?'=>$sku));
	}
	
	public function listAll($title,$hasen,$sku,$page=1,$limit=10)
	{
		$select = $this->table->select();
		if(!empty($title)){
			$slice = explode(" ", trim($title));
			foreach($slice as $v){
				$select->where('title like ?','%'.$v.'%');
			}
		}
		if(!empty($sku)){
			$select->where('sku = ?',$sku);
		}
		if(!empty($hasen)){
			if($hasen == '1'){
			    $select->where('en_title = ?','');
			}
		}
		$select->order('stock_id DESC');//降序就DESC
		$offset = ($page - 1) * $limit;
		$select->limit($limit,$offset);
        return $this->table->fetchAll($select);
	}
	
	public function getByTitle($title)
	{
		$select = $this->table->select();
		if(!empty($title)){
			$slice = explode(" ", trim($title));
			foreach($slice as $v){
				$select->where('title like ?','%'.$v.'%');
			}
				
		}
		$select->from($this->table,'sku');
		return $this->table->fetchAll($select);
	}
	
	public function getOne($id)
	{
		$row = $this->table->find($id);
		if ($row) {
			return $row->current();
		}
		return array();
	}
	
	/**获取低库存的产品*/
	public function getLowStock()
	{
		$sql = 'select v.value, v.warning_value, v.size, s.sku, s.title, s.is_forlong from stock_value v inner join stock s on s.sku=v.sku where v.value < v.warning_value order by sku';
		return $this->table->getAdapter()->fetchAll($sql);
	}
	
	public function getTotalNum($sku,$title,$hasen)
	{
		$where="where 1 ";
		if(!empty($sku)){
			$where.=" and sku='$sku'";
		}
		if(!empty($hasen)){
			if($hasen == 1){
			    $where.=" and en_title=''";
			}
		}
		if(!empty($title)){
			$arr = explode(' ', $title);//如果输入的title搜索有空格隔开，则分割字符串然后再生成语句
			foreach ($arr as $v){
				$where.=" and title like '%$v%'";
			}
		}
		return $this->table->getAdapter()->fetchOne("select count(*) from stock ".$where);
	}
	
	public function getBySku($sku)
	{
		$db= $this->table->getAdapter();
		$where = $db->quoteInto('sku in (?)',$sku);
		$rows = $this->table->fetchAll($where);
		return $rows;
	}
	
	/**从gogoalshop获取英文标题*/
	public function getEnSubject($sku)
	{
		$url="http://www.gogoalshop.com/product-getsubject?sku=".$sku;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 100);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$content = curl_exec($ch);
		curl_close($ch);
		return $content;
	}
	
	public function getAllStock()
	{
		$select = $this->table->select();
		$select->from($this->table,'*');
		return $this->table->fetchAll($select);
	}
	
	public function listEmptyEntitle()
	{
		$select = $this->table->select();
		$select->where('en_title = ?','');
		return $this->table->fetchAll($select);
	}
}